package com.newenergy.wisehome.ui.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import com.newenergy.wisehome.R;
import com.newenergy.wisehome.common.helper.SPUtil;
import com.newenergy.wisehome.config.AppManager;
import com.newenergy.wisehome.config.Constants;
import com.newenergy.wisehome.data.bean.DeviceCategory;

import butterknife.BindView;
import butterknife.OnClick;
import me.yokeyword.fragmentation.ISupportFragment;

/**
 * 加湿器
 * Created by kyx on 2018/8/13.
 */

public class HumidifierFragment extends BaseFragment implements SeekBar.OnSeekBarChangeListener{

    private static String ARG_HUMIDIFIER="arg_humidifier";

    @BindView(R.id.btn_back)
    Button mBtnBack;
    @BindView(R.id.tv_back)
    TextView mTvBack;
    @BindView(R.id.tv_page)
    TextView mTvPage;
    @BindView(R.id.seekbar_wl)
    SeekBar mSeekbarWl;
    @BindView(R.id.btn_open)
    Button mBtnOpen;
    @BindView(R.id.btn_off)
    Button mBtnOff;
    @BindView(R.id.btn_fixed)
    Button mBtnFixed;
    @BindView(R.id.btn_sleep)
    Button mBtnSleep;
    @BindView(R.id.btn_timming)
    Button mBtnTimming;
    @BindView(R.id.btn_night_light)
    Button mBtnNightLight;

    private Dialog mDialog;
    private TextView mTvTime;
    private Button mBtnTime;

    protected DeviceCategory mDeviceCategory;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_dehumidifier;
    }


    public static ISupportFragment newInstance(DeviceCategory mainItem) {
        Bundle args = new Bundle();
        args.putParcelable(ARG_HUMIDIFIER, mainItem);
        HumidifierFragment fragment = new HumidifierFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void init(View rootView) {
        mDeviceCategory = getArguments().getParcelable(ARG_HUMIDIFIER);

        mTvPage.setText(mDeviceCategory.getName());
        mSeekbarWl.setOnSeekBarChangeListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mDeviceCategory != null) {
            AppManager.getInstance(getContext()).mSelectDeviceCategory = mDeviceCategory;
        }
    }

    @OnClick({R.id.btn_back, R.id.tv_back, R.id.btn_open, R.id.btn_off, R.id.btn_fixed, R.id.btn_sleep, R.id.btn_timming, R.id.btn_night_light})
    public void onViewClicked(View view) {

        int key =0;
        int fkey =0;
        boolean isOnFixed = SPUtil.getBool(Constants.ISONFIXED,true);
        boolean isOnSleep = SPUtil.getBool(Constants.ISONSLEEP,true);
        boolean isOnNightLight = SPUtil.getBool(Constants.ISONNIGHTLIGHT,true);


        switch (view.getId()) {
            case R.id.btn_back:
                doBack();

                break;
            case R.id.tv_back:
                doBack();

                break;
            case R.id.btn_open:
                key =0x41;
                fkey = (key ^ 0xff);
                SPUtil.put(Constants.ISONHUMIDIFIER, false);


                break;
            case R.id.btn_off:
                key =0x42;
                fkey = (key ^ 0xff);
                SPUtil.put(Constants.ISONHUMIDIFIER, true);


                break;
            case R.id.btn_fixed:
                key = isOnFixed ? 0x61:0x62;
                fkey = (key ^ 0xff);
                SPUtil.put(Constants.ISONFIXED,!isOnFixed);
                mBtnFixed.setSelected(isOnFixed);
                break;
            case R.id.btn_sleep:
                key = isOnSleep ? 0x51 :0x52;
                fkey = (key ^ 0xff);
                SPUtil.put(Constants.ISONSLEEP,!isOnSleep);
                mBtnSleep.setSelected(isOnSleep);
                break;
            case R.id.btn_timming:
                showBottomDialog();
                return;

            case R.id.btn_night_light:
                key = isOnNightLight ? 0x71 : 0x72;
                fkey = (key ^ 0xff);
                SPUtil.put(Constants.ISONNIGHTLIGHT,!isOnNightLight);
                mBtnNightLight.setSelected(isOnNightLight);
                break;
        }
        String keyHex= getByteHex(key)+getByteHex(fkey);
        Log.e(TAG, "getByteHex: keyHex="+keyHex );
        AppManager.getInstance(mainActivity).commandAdvertise("02",mDeviceCategory.getFamilyId(),mDeviceCategory.getGroupId(),"137F"+keyHex,"000000",mCurrFamily.getSeed(),mCurrFamily.getFeedback(),1,"720D");

    }




    public void showBottomDialog() {
        View contenView = View.inflate(getContext(), R.layout.dialog_timer, null);
        mDialog = new Dialog(getContext() ,R.style.style_dialog);
        mDialog.setContentView(contenView);
        Window window = mDialog.getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);

        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        window.setAttributes(lp);

        Button cutTime = (Button) contenView.findViewById(R.id
                .btn_cut_time);
        mBtnTime = (Button) contenView.findViewById(R.id
                .btn_time);
        Button btnAddTime = (Button) contenView.findViewById(R.id
                .btn_add_time);

        Button btnTimer = (Button) contenView.findViewById(R.id
                .btn_timer);

        Button btnCancelTimer = (Button) contenView.findViewById(R.id
                .btn_cancel_timer);

        mBtnTime.setText(getString(R.string.one_hour));
//        UpdateTimeDesc("1小时");


        mDialog.show();


        cutTime.setOnClickListener(v -> {
            cutTime(mBtnTime.getText().toString());
//            UpdateTimeDesc(mBtnTime.getText().toString());
        });

        btnAddTime.setOnClickListener(v -> {
            addTime(mBtnTime.getText().toString());
//            UpdateTimeDesc(mBtnTime.getText().toString());

        });


        btnTimer.setOnClickListener(v -> {
            setTime();


            mDialog.dismiss();
        });


        btnCancelTimer.setOnClickListener(v -> {
            int key =0x20;
            int fkey = (key ^ 0xff);
            String keyHex= getByteHex(key)+getByteHex(fkey);
            Log.e(TAG, "getByteHex: keyHex="+keyHex );
            AppManager.getInstance(mainActivity).commandAdvertise("02",mDeviceCategory.getFamilyId(),mDeviceCategory.getGroupId(),"137F"+keyHex,"000000",mCurrFamily.getSeed(),mCurrFamily.getFeedback(),1,"720D");
            mBtnTimming.setSelected(false);
            mDialog.dismiss();
        });

    }

    private void setTime() {
        String time = mBtnTime.getText().toString();
        int key =0;
        if (time.equals(getString(R.string.one_hour))) {
            key = 0x21;
        } else if (time.equals(getString(R.string.two_hour))) {
            key = 0x22;
        }else if (time.equals(getString(R.string.four_hour))) {
            key = 0x24;
        }else if (time.equals(getString(R.string.eight_hour))) {
            key = 0x28;
        }
        int fkey = (key ^ 0xff);
        String keyHex= getByteHex(key)+getByteHex(fkey);
        Log.e(TAG, "getByteHex: keyHex="+keyHex );
        AppManager.getInstance(mainActivity).commandAdvertise("02",mDeviceCategory.getFamilyId(),mDeviceCategory.getGroupId(),"137F"+keyHex,"000000",mCurrFamily.getSeed(),mCurrFamily.getFeedback(),1,"720D");
        mBtnTimming.setSelected(true);


    }


    private void addTime(String setTime) {
        if (setTime.equals(getString(R.string.one_hour))) {
            mBtnTime.setText(getString(R.string.two_hour));
        } else if (setTime.equals(getString(R.string.two_hour))){
            mBtnTime.setText(getString(R.string.four_hour));
        }else if (setTime.equals(getString(R.string.four_hour))){
            mBtnTime.setText(getString(R.string.eight_hour));
        }else if (setTime.equals(getString(R.string.eight_hour))){
            mBtnTime.setText(getString(R.string.eight_hour));
        }
    }


    private void cutTime(String setTime) {
        if (setTime.equals(getString(R.string.one_hour))) {
            mBtnTime.setText(getString(R.string.one_hour));
        } else if (setTime.equals(getString(R.string.two_hour))){
            mBtnTime.setText(getString(R.string.one_hour));
        }else if (setTime.equals(getString(R.string.four_hour))){
            mBtnTime.setText(getString(R.string.two_hour));
        }else if (setTime.equals(getString(R.string.eight_hour))){
            mBtnTime.setText(getString(R.string.four_hour));
        }

    }


    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        int progress = mSeekbarWl.getProgress();
        int key =0;

        if (0 < progress && progress <=25) {
            key =0x34;

        } else if (25 < progress && progress <= 50) {
            key =0x33;

        }else if (50 < progress && progress <= 75) {
            key =0x32;

        }else if (75 < progress && progress <= 100) {
            key =0x31;

        }
        int fkey = (key ^ 0xff);
        String keyHex= getByteHex(key)+getByteHex(fkey);
        Log.e(TAG, "getByteHex: keyHex="+keyHex );
        AppManager.getInstance(mainActivity).commandAdvertise("02",mDeviceCategory.getFamilyId(),mDeviceCategory.getGroupId(),"137F"+keyHex,"000000",mCurrFamily.getSeed(),mCurrFamily.getFeedback(),1,"720D");

    }






















/*    private String UpdateTimeDesc(String setTime) {
        //获取当前的时间
        Calendar calendar = Calendar.getInstance();

        long setHour = 0;
        if (setTime.equals("1小时")) {
            setHour = 1;
        } else if (setTime.equals("2小时")){
            setHour = 2;
        }else if (setTime.equals("4小时")){
            setHour = 4;
        }else if (setTime.equals("8小时")){
            setHour = 8;
        }
        long nowHour = calendar.get(Calendar.HOUR_OF_DAY);
        long nowMinute = calendar.get(Calendar.MINUTE);
//        long nowSecond = calendar.get(Calendar.SECOND);

        long endHour = nowHour + setHour;

        String noon ;
        if (endHour > 12) {
            noon = "下午";
            endHour = endHour-12;
        }else {
            noon = "上午";
        }


        String endMinute = "";
        if (nowMinute <= 9) {
            endMinute = "0" + nowMinute;
        }else {
            endMinute = nowMinute + "";
        }

       *//* String endSecond = "";
        if (nowMinute <= 9) {
            endSecond = "0" + nowSecond;
        }else {
            endSecond = nowSecond + "";
        }*//*

        String desTime = noon + " " + endHour + ":" + endMinute  + "关机";

        return desTime;

    }*/
}
